package com.lys.eduservice.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.eduservice.entity.EduCourse;
import com.lys.eduservice.entity.EduCourseDescription;
import com.lys.eduservice.entity.frontvo.CourseQueryVo;
import com.lys.eduservice.entity.frontvo.CourseWebVo;
import com.lys.eduservice.entity.vo.CoursePublishVo;
import com.lys.eduservice.entity.vo.CourseQuery;
import com.lys.eduservice.entity.vo.SaveCourseVo;
import com.lys.eduservice.mapper.EduCourseMapper;
import com.lys.eduservice.service.EduChapterService;
import com.lys.eduservice.service.EduCourseDescriptionService;
import com.lys.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lys.eduservice.service.EduVideoService;
import com.lys.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Lyyyys
 * @since 2021-04-30
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseMapper mapper;
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;


    @Override
    public String saveCourseInfo(SaveCourseVo courseVo) {
        //先要向课程表添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseVo,eduCourse);
        int insert = mapper.insert(eduCourse);
        if(insert == 0){
            throw new GuliException(20001,"课程创建失败");
        }

        //根据添加成功后的课程表中的到的ID值向课程描述表中添加课程描述信息
        EduCourseDescription description = new EduCourseDescription();
        description.setId(eduCourse.getId());
        description.setDescription(courseVo.getDescription());
        boolean resultDescription = eduCourseDescriptionService.save(description);
        if(!resultDescription){
            throw new GuliException(20001,"课程详情保存失败");
        }
        return eduCourse.getId();
    }

    @Override
    public CoursePublishVo getCoursePublishVoByCourseId(String id) {
        CoursePublishVo coursePublishVo = mapper.getCoursePublishVoByCourseId(id);
        return coursePublishVo;
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }

        mapper.selectPage(pageParam, queryWrapper);
    }

    @Transactional
    @Override
    public boolean removeCourseById(String id) {
        //根据课程id删除小节
        eduVideoService.deleteVideoByCourseId(id);

        //根据课程id删除章节
        eduChapterService.deleteChapterByCourseId(id);

        //根据课程id删除描述
        eduCourseDescriptionService.removeById(id);

        //根据课程id删除课程本身
        int result = mapper.deleteById(id);

        return result > 0;
    }

    @Override
    public List<EduCourse> getHotCourseLimit() {
        List<EduCourse> list = mapper.getHotCourseLimit();
        return list;
    }

    /**
     * 根据讲师id查询当前讲师的课程列表
     * @param teacherId
     * @return
     */
    @Override
    public List<EduCourse> selectByTeacherId(String teacherId) {

        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<EduCourse>();

        queryWrapper.eq("teacher_id", teacherId);
        //按照最后更新时间倒序排列
        queryWrapper.orderByDesc("gmt_modified");

        List<EduCourse> courses = baseMapper.selectList(queryWrapper);
        return courses;
    }

    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {//一级分类
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {//二级分类
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {//关注度
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {//最新
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {//价格
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, queryWrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return mapper.getBaseCourseInfo(courseId);
    }


}
